package tech.abing.spider.task.pageprocessor.sac;

import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.sac.SecComEntity;
import tech.abing.spider.task.utils.TaskUtil;

public class SecuritiesCompanyProcessor implements PageProcessor  {

	private static final String SEARCH_URL = "http://jg.sac.net.cn/pages/publicity/resource!search.action";
	private static final String LIST_URL = "http://jg.sac.net.cn/pages/publicity/resource!list.action";
	private static Logger logger = Logger.getLogger(SecuritiesCompanyProcessor.class);
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}
	 
	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		JSONArray jsonArray = JSONArray.fromObject(page.getContent());
		
		PageRequest request = page.getRequest();
		
		// ----------------test:
		/*PageRequest ormReq = context.createPageRequest(SEARCH_URL, SecuritiesCompanyInfoProcessor.class);
		
		SecComEntity sc = new SecComEntity();
		
		sc.setAoiId("4629");
		sc.setAoiName("金通证券有限责任公司");
		sc.setOtcId("01");
		sc.setRnum(1);
		sc.setSacId("06");
		
		ormReq.setMethod(request.getMethod());
		ormReq.setPostType(request.getPostType());
//		ormReq.setHeaders(request.getHeaders());
		
		ormReq.putHeader("Accept-Encoding","gzip, deflate");
		ormReq.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
		ormReq.putHeader("Connection", "keep-alive");
		ormReq.putHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ormReq.putHeader("Cookie","BIGipServerjigou=1095805120.20480.0000; JSESSIONID=XrKcYGrdZN2kFlnG0BwPwJy0Ql0Bv8Mhv1RGzBLQZbQBzJlTpgdh!-1193698414");
		ormReq.putHeader("Host","jg.sac.net.cn");
		ormReq.putHeader("Origin","http://jg.sac.net.cn");
//		ormReq.putHeader("Referer","http://jg.sac.net.cn/pages/publicity/securities-list.html");
		ormReq.putHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
		ormReq.putHeader("X-Requested-With", "XMLHttpRequest");
		
		ormReq.putHeader("Referer", "http://jg.sac.net.cn/pages/publicity/securities_reg.html?aoi_id="+String.valueOf(4629)+"&is_org_search=no");
		
		ormReq.putParams("filter_EQS_AOI#AOI_ID", "4629");
		ormReq.putParams("sqlkey", "publicity");
		ormReq.putParams("sqlval", "SELECT_ZQ_ORM_INFO");
		
		ormReq.getExtParams().put("sc", sc);
			
		queue.add(ormReq);*/
		//---------test over -----------------
		for(int i = 0;i< jsonArray.size();i++){
			SecComEntity sc = new SecComEntity();
			JSONObject retObj = jsonArray.getJSONObject(i);
			String aoiId = retObj.getString("AOI_ID");
			sc.setAoiId(aoiId);
			sc.setAoiName(retObj.getString("AOI_NAME"));
			sc.setOtcId(retObj.getString("OTC_ID"));
			sc.setRnum(retObj.getInt("RNUM"));
			sc.setSacId(retObj.getString("SAC_ID"));
			
			logger.info("当前数据：{aoi_id："+aoiId+",aoi_name:"+retObj.getString("AOI_NAME")+"}");
			// ------------基本信息-------------------
			PageRequest infoReq = context.createPageRequest(SEARCH_URL, SecuritiesCompanyInfoProcessor.class);
			
			infoReq.setMethod(request.getMethod());
			infoReq.setPostType(request.getPostType());
			infoReq.setHeaders(request.getHeaders());
			infoReq.putHeader("Referer", "http://jg.sac.net.cn/pages/publicity/securities_reg.html?aoi_id="+aoiId+"&is_org_search=no");
			
			infoReq.putParams("filter_EQS_aoi_id", aoiId);
			infoReq.putParams("sqlkey", "publicity");
			infoReq.putParams("sqlval", "SELECT_ZQ_REG_INFO");
			
			infoReq.getExtParams().put("sc", sc);
			
			queue.add(infoReq);
			
			// --------------特殊请求:
			/*PageRequest ormReq = context.createPageRequest(SEARCH_URL, SecuritiesCompanyInfoProcessor.class);
				
			ormReq.setMethod(request.getMethod());
			ormReq.setPostType(request.getPostType());
			ormReq.setHeaders(request.getHeaders());
			ormReq.putHeader("Referer", "http://jg.sac.net.cn/pages/publicity/securities_reg.html?aoi_id="+aoiId+"&is_org_search=no");
			
			ormReq.putParams("filter_EQS_AOI#AOI_ID", aoiId);
			ormReq.putParams("sqlkey", "publicity");
			ormReq.putParams("sqlval", "SELECT_ZQ_ORM_INFO");
			
			ormReq.getExtParams().put("sc", sc);
				
			queue.add(ormReq);*/
			
			
			// ------------资格信息-------------------
			PageRequest quaReq = context.createPageRequest(SEARCH_URL, SecuritiesCompanyQualificationsProcessor.class);
			
			quaReq.setMethod(request.getMethod());
			quaReq.setPostType(request.getPostType());
			quaReq.setHeaders(request.getHeaders());
			quaReq.putHeader("Referer", "http://jg.sac.net.cn/pages/publicity/securities_reg.html?aoi_id="+aoiId+"&is_org_search=no");
			
			quaReq.putParams("filter_EQS_aoi_id", aoiId);
			quaReq.putParams("sqlkey", "publicity");
			quaReq.putParams("sqlval", "SEARCH_ZQGS_QUALIFATION");
			
			quaReq.getExtParams().put("aoiId", aoiId);
			
			queue.add(quaReq);
			
			// -----------分公司---------------------
			PageRequest subReq = context.createPageRequest(LIST_URL, SecuritiesSubCompanyProcessor.class);
			subReq.setMethod(request.getMethod());
			subReq.setPostType(request.getPostType());
			subReq.setHeaders(request.getHeaders());
			subReq.putHeader("Referer", "http://jg.sac.net.cn/pages/publicity/securities-branch-org.html?aoi_id="+aoiId+"&is_org_search=no");
			
			subReq.putParams("filter_LIKES_mboi_branch_full_name", "");
			subReq.putParams("filter_LIKES_mboi_off_address", "");
			subReq.putParams("filter_EQS_aoi_id", aoiId);
			
			subReq.putParams("page.searchFileName", "publicity");
			subReq.putParams("page.sqlKey", "PAG_BRANCH_ORG");
			subReq.putParams("page.sqlCKey", "SIZE_BRANCH_ORG");
			subReq.putParams("_search", "false");
			subReq.putParams("nd",TaskUtil.getRandomStrByDate());
			subReq.putParams("page.pageSize", "15");
			subReq.putParams("page.pageNo", "1");
			subReq.putParams("page.orderBy", "MATO_UPDATE_DATE");
			subReq.putParams("page.order", "desc");
			
			subReq.getExtParams().put("aoiId", aoiId);
			
			queue.add(subReq);
			
			// ------------ 营业部 -------------------
			
			PageRequest saleReq = context.createPageRequest(LIST_URL, SecuritiesSaleDepProcessor.class);
			saleReq.setMethod(request.getMethod());
			saleReq.setPostType(request.getPostType());
			saleReq.setHeaders(request.getHeaders());
			saleReq.putHeader("Referer", "http://jg.sac.net.cn/pages/publicity/securities-sales-dept.html?aoi_id="+aoiId+"&is_org_search=no");
			
			saleReq.putParams("filter_LIKES_msdi_name", "");
			saleReq.putParams("filter_LIKES_msdi_reg_address", "");
			saleReq.putParams("filter_EQS_aoi_id", aoiId);
			saleReq.putParams("page.searchFileName", "publicity");
			saleReq.putParams("page.sqlKey", "PAG_SALES_DEPT");
			saleReq.putParams("page.sqlCKey", "SIZE_SALES_DEPT");
			saleReq.putParams("_search", "false");
			saleReq.putParams("nd",TaskUtil.getRandomStrByDate());
			saleReq.putParams("page.pageSize", "15");
			saleReq.putParams("page.pageNo", "1");
			saleReq.putParams("page.orderBy", "MATO_UPDATE_DATE");
			saleReq.putParams("page.order", "desc");
			
			saleReq.getExtParams().put("aoiId", aoiId);
			
			queue.add(saleReq);
			
			//------------- 高管信息 -----------------------------
			PageRequest exeReq = context.createPageRequest(SEARCH_URL, SecuritiesExecutiveProcessor.class);
			
			exeReq.setMethod(request.getMethod());
			exeReq.setPostType(request.getPostType());
			exeReq.setHeaders(request.getHeaders());
			exeReq.putHeader("Referer", "http://jg.sac.net.cn/pages/publicity/executive-list.html?aoi_id="+aoiId+"&is_org_search=no");
			
			exeReq.putParams("filter_EQS_aoi_id", aoiId);
			exeReq.putParams("sqlkey", "publicity");
			exeReq.putParams("sqlval", "EXECUTIVE_LIST");
			
			exeReq.getExtParams().put("aoiId", aoiId);
			
			queue.add(exeReq);
		}
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
		
	}

}
